package com.wayne.blog.common;

import cn.dev33.satoken.exception.NotLoginException;
import cn.dev33.satoken.exception.NotPermissionException;
import cn.dev33.satoken.exception.NotRoleException;
import com.wayne.blog.exception.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局异常处理器
 * @author Lang
 * @since 2024/06/06
 */
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {

    /**
     * 处理未登录异常
     */
    @ExceptionHandler(NotLoginException.class)
    public Ret<?> handleNotLoginException(NotLoginException e) {
        log.error("未登录异常", e);
        return Ret.error(401, "请先登录");
    }

    /**
     * 处理无权限异常
     */
    @ExceptionHandler(NotPermissionException.class)
    public Ret<?> handleNotPermissionException(NotPermissionException e) {
        log.error("无权限异常", e);
        return Ret.error(403, "无权限访问");
    }

    /**
     * 处理无角色异常
     */
    @ExceptionHandler(NotRoleException.class)
    public Ret<?> handleNotRoleException(NotRoleException e) {
        log.error("无角色异常", e);
        return Ret.error(403, "无角色访问");
    }

    /**
     * 处理业务异常
     */
    @ExceptionHandler(BusinessException.class)
    public Ret<?> handleBusinessException(BusinessException e) {
        log.error("业务异常", e);
        return Ret.error(e.getCode(), e.getMessage());
    }

    /**
     * 处理其他异常
     */
    @ExceptionHandler(Exception.class)
    public Ret<?> handleException(Exception e) {
        log.error("系统异常", e);
        return Ret.error(500, "系统异常");
    }
} 